﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using HtmlAgilityPack;
using System.Web;

namespace iLyrics
{
    class LyricFinder
    {
        public enum Options
        {
            All,
            ChartLyrics,
            LyricWiki,
            LYRDB
        };

        private Options searchOption;

        public LyricFinder()
        {
            searchOption = Options.ChartLyrics;
        }

        public LyricFinder(Options o)
        {
            searchOption = o;
        }

        public string getLyrics(string Artist, string TrackName)
        {
            string lyrics = null;
            switch (searchOption)
            {
                case Options.All:
                    {
                        lyrics = LyricWiki(Artist, TrackName);
                        break;
                    }
                case Options.ChartLyrics:
                    {
                        lyrics = ChartLyrics(Artist, TrackName);
                        break;
                    }
                case Options.LyricWiki:
                    {
                        lyrics = LyricWiki(Artist, TrackName);
                        break;
                    }
                case Options.LYRDB:
                    {
                        lyrics = LYRDB(Artist, TrackName);
                        break;
                    }
            }
            return lyrics;
        }

        private string LyricWiki(string Artist, string TrackName)
        {
            string lyrics = null;
            try
            {
                com.wikia.lyrics.LyricWiki wiki = new com.wikia.lyrics.LyricWiki();
                com.wikia.lyrics.LyricsResult result = wiki.getSongResult(Artist, TrackName);
                if (result != null)
                {
                    using (WebClient wc = new WebClient())
                    {
                        string html = wc.DownloadString(result.url);
                        html = html.Replace("<br />", "\r\n");
                        HtmlDocument doc = new HtmlDocument();
                        doc.LoadHtml(html);
                        HtmlNode lyricNode = doc.DocumentNode.SelectSingleNode("//div[@class='lyricbox']");
                        if (lyricNode != null)
                        {
                            int start = lyricNode.InnerText.IndexOf("&#");
                            int end = lyricNode.InnerText.LastIndexOf(";") + 1;
                            lyrics = lyricNode.InnerText.Substring(start, end - start);
                            lyrics = System.Net.WebUtility.HtmlDecode(lyrics);
                        }
                    }
                }
                return lyrics;
            }
            catch
            {
                return null;
            }
        }

        private string LYRDB(string Artist, string TrackName)
        {
            string lyrics = null;
            try
            {
                using (WebClient wc = new WebClient())
                {

                    string request = wc.DownloadString("http://webservices.lyrdb.com/lookup.php?q="+Artist+"|"+TrackName+"&for=match&agent=iLyrics/0.1");
                    if (request == null)
                        return null;
                    lyrics = wc.DownloadString("http://www.lyrdb.com/getlyr.php?q=" + request.Split('\\')[0]);
                    return lyrics.StartsWith(null) ? null : lyrics;
                }
            }
            catch
            {
                return null;
            }
        }

        private string ChartLyrics(string Artist, string TrackName)
        {
            string lyrics = null;

            try
            {
                com.chartlyrics.api.apiv1 clapi = new com.chartlyrics.api.apiv1();
                com.chartlyrics.api.SearchLyricResult[] searchResult = clapi.SearchLyric(Artist.ToLower(), TrackName.ToLower());
                if (searchResult.Length > 0)
                {
                    com.chartlyrics.api.GetLyricResult lyricResult = clapi.GetLyric(searchResult[0].LyricId, searchResult[0].LyricChecksum);
                    lyrics = lyricResult.Lyric;
                }

            }
            catch
            {
                return null;
            }

            return lyrics;
        }
    }
}
